package com.ydx.meituan.model.Annotations;

import com.ydx.meituan.utils.ParamsCheckUtil;
import javax.validation.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
*
* Description:timeStamp校验
*
* */
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = YdxTimestampCheckValidator.class)
public @interface YdxTimestampCheck {

    String message() default "";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

class YdxTimestampCheckValidator implements ConstraintValidator<YdxTimestampCheck,Long> {

    public String PARAMS_NAME="";

    @Override
    public void initialize(YdxTimestampCheck constraintAnnotation) {
    }

    @Override
    public boolean isValid(Long value, ConstraintValidatorContext constraintValidatorContext) {
        PARAMS_NAME = ParamsCheckUtil.getParamsName(constraintValidatorContext);
        constraintValidatorContext.disableDefaultConstraintViolation();
        if (value==null){
            constraintValidatorContext.buildConstraintViolationWithTemplate(PARAMS_NAME + "不能为空").addConstraintViolation();
            return false;
        }
        if (!(value>=1000000000000l&&value<=9999999999999l)){
            constraintValidatorContext.buildConstraintViolationWithTemplate(PARAMS_NAME+"请传入13位毫秒时间戳").addConstraintViolation();
            return false;
        }
        return true;
    }
}
